home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 7 / Amiga Format AFCD07 (Dec 1996, Issue 91).iso / serious / shareware / programming / emacs-complete / fsf / emacs / lisp / saveplace.el < prev    next >
Lisp/Scheme  |  1994-09-05  |  8KB  |  200 lines

  1. ;;; saveplace.el --- automatically save place in files.
  2.  
  3. ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Karl Fogel <kfogel@cs.oberlin.edu>
  6. ;; Maintainer: FSF
  7. ;; Created: July, 1993
  8. ;; Version: 1.2
  9. ;; Keywords: bookmarks, placeholders
  10.  
  11. ;; This file is part of GNU Emacs.
  12.  
  13. ;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;; GNU General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  25. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  
  27. ;; Automatically save place in files, so that visiting them later
  28. ;; (even during a different Emacs session) automatically moves point
  29. ;; to the saved position, when the file is first found.  Uses the
  30. ;; value of buffer-local variable save-place to determine whether to
  31. ;; save position or not.
  32. ;;
  33. ;; Thanks to Stefan Schoef, who sent a patch with the
  34. ;; `save-place-version-control' stuff in it.
  35.  
  36. ;; this is what I was using during testing:
  37. ;; (define-key ctl-x-map "p" 'toggle-save-place)
  38.  
  39. (defvar save-place-alist nil
  40.   "Alist of saved places to go back to when revisiting files.
  41. Each element looks like (FILENAME . POSITION);
  42. visiting file FILENAME goes automatically to position POSITION
  43. rather than the beginning of the buffer.
  44. This alist is saved between Emacs sessions.")
  45.  
  46. (defvar save-place nil
  47.   "*Non-nil means automatically save place in each file.
  48. This means when you visit a file, point goes to the last place
  49. where it was when you previously visited the same file.
  50. This variable is automatically buffer-local.
  51.  
  52. If you wish your place in any file to always be automatically saved,
  53. simply put this in your `~/.emacs' file:
  54.  
  55. \(setq-default save-place t\)")
  56.  
  57. (make-variable-buffer-local 'save-place)
  58.  
  59. (defvar save-place-file "~/.emacs-places"
  60.   "*Name of the file that records `save-place-alist' value.")
  61.  
  62. (defvar save-place-version-control 'nospecial
  63.   "*Controls whether to make numbered backups of master save-place file.
  64. It can have four values: t, nil, `never', and `nospecial'.  The first
  65. three have the same meaning that they do for the variable
  66. `version-control', and the final value `nospecial' means just use the
  67. value of `version-control'.")
  68.  
  69. (defvar save-place-loaded nil
  70.   "Non-nil means that the `save-place-file' has been loaded.")
  71.  
  72. (defun toggle-save-place (&optional parg)
  73.   "Toggle whether to save your place in this file between sessions.
  74. If this mode is enabled, point is recorded when you kill the buffer
  75. or exit Emacs.  Visiting this file again will go to that position,
  76. even in a later Emacs session.
  77.  
  78. If called with a prefix arg, the mode is enabled if and only if
  79. the argument is positive.
  80.  
  81. To save places automatically in all files, put this in your `.emacs' file:
  82.  
  83. \(setq-default save-place t\)"
  84.   (interactive "P")
  85.   (if (not buffer-file-name)
  86.       (message 
  87.        (format "Buffer \"%s\" not visiting a file." (buffer-name)))
  88.     (if (and save-place (or (not parg) (<= parg 0)))
  89.     (progn
  90.       (message "No place will be saved in this file.")
  91.       (setq save-place nil))
  92.       (message "Place will be saved.")
  93.       (setq save-place t))))
  94.  
  95. (defun save-place-to-alist ()
  96.   ;; put filename and point in a cons box and then cons that onto the
  97.   ;; front of the save-place-alist, if save-place is non-nil.
  98.   ;; Otherwise, just delete that file from the alist.
  99.   ;; first check to make sure alist has been loaded in from the master
  100.   ;; file.  If not, do so, then feel free to modify the alist.  It
  101.   ;; will be saved again when Emacs is killed.
  102.   (or save-place-loaded (load-save-place-alist-from-file))
  103.   (if buffer-file-name
  104.       (progn
  105.         (let ((cell (assoc buffer-file-name save-place-alist)))
  106.           (if cell
  107.               (setq save-place-alist (delq cell save-place-alist))))
  108.         (if save-place
  109.             (setq save-place-alist
  110.                   (cons (cons buffer-file-name (point))
  111.                         save-place-alist))))))
  112.  
  113. (defun save-place-alist-to-file ()
  114.   (let ((file (expand-file-name save-place-file)))
  115.     (save-excursion
  116.       (message (format "Saving places to %s..." file))
  117.       (set-buffer (get-buffer-create " *Saved Places*"))
  118.       (delete-region (point-min) (point-max))
  119.       (if (file-readable-p file)
  120.           (insert-file-contents file))
  121.       (delete-region (point-min) (point-max))
  122.       (goto-char (point-min))
  123.       (print save-place-alist (current-buffer))
  124.       (let ((version-control
  125.              (cond
  126.               ((null save-place-version-control) nil)
  127.               ((eq 'never save-place-version-control) 'never)
  128.               ((eq 'nospecial save-place-version-control) version-control)
  129.               (t
  130.                t))))
  131.         (write-file file)
  132.         (kill-buffer (current-buffer))
  133.         (message (format "Saving places to %s... done." file))))))
  134.  
  135. (defun load-save-place-alist-from-file ()
  136.   (if (not save-place-loaded)
  137.       (progn
  138.         (setq save-place-loaded t)
  139.         (let ((file (expand-file-name save-place-file)))
  140.           ;; make sure that the alist does not get overwritten, and then
  141.           ;; load it if it exists:
  142.           (if (file-readable-p file)
  143.               (save-excursion
  144.                 (message (format "Loading places from %s..."
  145.                                  save-place-file))
  146.                 ;; don't want to use find-file because we have been
  147.                 ;; adding hooks to it.
  148.                 (set-buffer (get-buffer-create " *Saved Places*"))
  149.                 (delete-region (point-min) (point-max))
  150.                 (insert-file-contents file)
  151.                 (goto-char (point-min))
  152.                 (setq save-place-alist 
  153.                       (car (read-from-string
  154.                             (buffer-substring (point-min) (point-max)))))
  155.                 (kill-buffer (current-buffer))
  156.                 (message (format "Loading places from %s... done." file))
  157.                 t)
  158.             t)
  159.           nil))))
  160.  
  161. (defun save-places-to-alist ()
  162.   ;; go through buffer-list, saving places to alist if save-place is
  163.   ;; non-nil, deleting them from alist if it is nil.
  164.   (let ((buf-list (buffer-list)))
  165.     (while buf-list
  166.       ;; put this into a save-excursion in case someone is counting on
  167.       ;; another function in kill-emacs-hook to act on the last buffer
  168.       ;; they were in:
  169.       (save-excursion
  170.     (set-buffer (car buf-list))
  171.     ;; save-place checks buffer-file-name too, but we can avoid
  172.     ;; overhead of function call by checking here too.
  173.     (and buffer-file-name (save-place-to-alist))
  174.     (setq buf-list (cdr buf-list))))))
  175.  
  176. (defun save-place-find-file-hook ()
  177.   (or save-place-loaded (load-save-place-alist-from-file))
  178.   (let ((cell (assoc buffer-file-name save-place-alist)))
  179.     (if cell
  180.         (progn
  181.       (or after-find-file-from-revert-buffer
  182.           (goto-char (cdr cell)))
  183.           ;; and make sure it will be saved again for later
  184.           (setq save-place t)))))
  185.  
  186. (defun save-place-kill-emacs-hook ()
  187.   (save-places-to-alist)
  188.   (save-place-alist-to-file))
  189.  
  190. (add-hook 'find-file-hooks 'save-place-find-file-hook t)
  191.  
  192. (add-hook 'kill-emacs-hook 'save-place-kill-emacs-hook)
  193.  
  194. (add-hook 'kill-buffer-hook 'save-place-to-alist)
  195.  
  196. (provide 'saveplace) ; why not...
  197.  
  198. ;;; saveplace.el ends here
  199.  
  200.